﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WisPortal.Data;
using System.Data;
using System.Xml;
using System.IO;

namespace WisPortal.Areas.Admin.Controllers
{
    public class DevController : Controller
    {
        //
        // GET: /Admin/Dev/

        public ActionResult Index()
        {
            if (HttpContext.Request["CreateObjectMapping"] != null)
            {
                if (HttpContext.Request["tables"] != null)
                {
                    DataMapping dm = new DataMapping(HttpContext);
                    dm.DatabaseType = DatabaseType.SQLServer;
                    XmlDocument doc = new XmlDocument();
                    XmlNode objNode = null;
                    if (Request["InvolveApplication"] != null)
                    {
                        objNode = dm.CreateApplicationNode(doc, 
                            Server.MapPath("~/Content/schema/DataObjectSchema.xsd"), 
                            Request["Title"] != null ? Request["Title"] : "", 
                            HttpContext.Request["tables"], 
                            HttpContext.Request["subtables"]);
                    }
                    else
                    {
                        objNode = dm.CreateBizObject(doc, HttpContext.Request["tables"], HttpContext.Request["subtables"]);                        
                    }
                    ViewData["DataMappingResult"] = objNode.OuterXml;
                }
                
            }
            else if (HttpContext.Request["CreateDDL"] != null)
            {
                return CreateDDLFile();
            }
            else if (HttpContext.Request["CreateUISet"] != null)
            {
                WisPortal.AppUICreator appUI = new WisPortal.AppUICreator();
                XmlNode filedSet = appUI.CreateFieldSetNode(HttpContext.Request["tables"]);
                ViewData["DataMappingResult"] = filedSet.OuterXml;
            }


            DataSet ds = DB.GetDataSet("SELECT * FROM [TSX].[INFORMATION_SCHEMA].[TABLES] ORDER BY TABLE_NAME");
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                SelectListItem item = new SelectListItem();
                item.Text = row["TABLE_NAME"].ToString();
                item.Value = row["TABLE_NAME"].ToString();
                items.Add(item);
            }
            ViewData.Add("tables", items);
            ViewData.Add("subtables", items);
            return View();
        }

        
        public ActionResult CreateDDLFile(DatabaseType dbType = DatabaseType.Sqlite)
        {
            List<string> tables = new List<string>();

            if (HttpContext.Request["tables"] != null)
            {
                tables.Add(HttpContext.Request["tables"]);
            }
            if (HttpContext.Request["subtables"] != null)
            {
                string[] subtables = HttpContext.Request["subtables"].Split(",".ToCharArray());
                foreach (string subtable in subtables)
                {
                    if (!tables.Contains(subtable))
                        tables.Add(subtable);
                }
            }
            if(tables.Count > 0)
            {
                DataMapping dm = new DataMapping(HttpContext);
                string content = dm.CreateDDL(tables.ToArray(), dbType);
                Stream s = new MemoryStream(System.Text.UTF8Encoding.Default.GetBytes(content));
                return File(s, "application/octet-stream", "ddl.sql");
            }
            else
                return Content("No tables selected");
        }

        [ValidateInput(false)]
        public ActionResult SqlTools()
        {
            if (Request["ProcessSql"] != null)
            {
                string sql = Request["sql"];
                ViewData["result"] = sql.Replace("\r\n", "").Replace("  ", " ").Trim();
            }
            return View();
        }
    }
}
